package com.iot08.api.service;

import com.alibaba.fastjson.JSONObject;
import com.iot08.api.service.supplement.KafkaService;
import com.iot08.api.vo.request.SedentaryReqVO;

import com.iot08.common.config.mongodb.query.UpdateVO;
import com.iot08.common.constant.ConstKafka;
import com.iot08.common.entity.app.Device;
import com.iot08.common.entity.app.DeviceSedentary;
import com.iot08.common.service.base.BaseService;
import com.iot08.common.utils.DateUtil;
import com.iot08.common.utils.response.ResponseMap;
import lombok.extern.slf4j.Slf4j;
import org.springframework.data.mongodb.core.query.Criteria;
import org.springframework.data.mongodb.core.query.Query;
import org.springframework.stereotype.Service;

import javax.annotation.Resource;
import java.util.Date;

/**
 * @version 1.0
 * @Description:
 * @author: Rock
 * @time: 2021/1/8 16:24
 */
@Slf4j
@Service
public class DeviceSedentaryService extends BaseService<DeviceSedentary> {

    @Resource
    private DeviceService deviceService;

    @Resource
    private KafkaService kafkaService;
    /**
     * @param imei
     * @time: 12/13/21 5:22 PM
     * @author: Weichao
     * @version 1.0
     */
    public DeviceSedentary findByImei(String imei) {
        return this.findById(imei);
    }

    @Override
    public DeviceSedentary save(DeviceSedentary entity) {
        entity.setUpDateTime(new Date());
        entity.setUpTime(DateUtil.nowTimestampToLong());
        return mongoTemplate.save(entity);
    }
    /**
     * 查/改久坐提醒
     * @param imei
     * @param vo
     * @time: 12/13/21 4:08 PM
     * @author: Weichao
     * @version 1.0
     */
    public ResponseMap sedentary(String imei, SedentaryReqVO vo) {
        JSONObject res = new JSONObject();
        DeviceSedentary devS = this.findByImei(imei);
        if(devS == null){
            //返回默认的 并创建
            DeviceSedentary newDevS = new DeviceSedentary();
            newDevS.setId(imei);
            newDevS.setAmStatus(0);
            newDevS.setAmSTime("08:00");
            newDevS.setAmETime("11:30");
            newDevS.setPmStatus(0);
            newDevS.setPmSTime("14:00");
            newDevS.setPmETime("18:00");
            newDevS.setCycleTime(60*60); //60分钟
            newDevS.setWeek("1111111"); //每天
            devS = this.save(newDevS);
        }
        if(vo.getOption().equals("get")){
            //查
            res.put("amStatus", devS.getAmStatus());
            res.put("amSTime", devS.getAmSTime());
            res.put("amETime", devS.getAmETime());
            res.put("pmStatus", devS.getPmStatus());
            res.put("pmSTime", devS.getPmSTime());
            res.put("pmETime", devS.getPmETime());
            res.put("cycleTime", devS.getCycleTime());
            res.put("week", devS.getWeek());
        }else if (vo.getOption().equals("change")){
            //改
            UpdateVO updateVO = new UpdateVO();
            updateVO.set("amStatus", vo.getAmStatus());
            updateVO.set("amSTime", vo.getAmSTime());
            updateVO.set("amETime", vo.getAmETime());
            updateVO.set("pmStatus", vo.getPmStatus());
            updateVO.set("pmSTime", vo.getPmSTime());
            updateVO.set("pmETime", vo.getPmETime());
            updateVO.set("cycleTime", vo.getCycleTime());
            updateVO.set("week", vo.getWeek());
            this.updateById(imei, updateVO);
            UpdateVO devUpdateVO = new UpdateVO();
            if(vo.getAmStatus() == 1 || vo.getPmStatus() == 1 ){
                devUpdateVO.set("sedentaryStatus", 1);
            }else{
                devUpdateVO.set("sedentaryStatus", 0);
            }
            deviceService.updateById(imei, devUpdateVO);
            // 下发修改久坐提醒的 指令
            JSONObject kafkaJson = new JSONObject();
            kafkaJson.put("amStatus", vo.getAmStatus());
            kafkaJson.put("amSTime", vo.getAmSTime());
            kafkaJson.put("amETime", vo.getAmETime());
            kafkaJson.put("pmStatus", vo.getPmStatus());
            kafkaJson.put("pmSTime", vo.getPmSTime());
            kafkaJson.put("pmETime", vo.getPmETime());
            kafkaJson.put("cycleTime", vo.getCycleTime());
            kafkaJson.put("week", vo.getWeek());
            //kafkaService.emitDeviceDemand(imei, ConstKafka.DEVICE_TYPE_122, kafkaJson, vo.getOpenid());
        }

        return ResponseMap.success(res);
    }
}
